import com.troyworks.framework.ui.BaseComponent;
import com.troyworks.calendar.Calendar;
import com.troyworks.hsmf.AEvent;
import com.troyworks.framework.model.BaseModelObject;
import com.troyworks.events.TProxy;
import com.troyworks.util.datetime.TDate;
import mx.controls.TextInput;
import com.troyworks.framework.ui.IHaveChildrenComponents;
import mx.transitions.OnEnterFrameBeacon;
import com.troyworks.events.EVTD;

/**
 * @author Troy Gardner
 */
class com.troyworks.framework.ui.DateField extends BaseComponent implements IHaveChildrenComponents {
	public var inputTxtA:TextInput;
	public var choose_btn:Button;
	public var cal:Calendar;
	
	function DateField(initialState : Function, hsmfName : String, aInit : Boolean) {
		super(initialState, "DateField", aInit);
		
				tabEnabled =true;
				tabChildren = true;
				focusEnabled = true;
	}
	public function onSelectedDateChanged():Void{
		var selected:String = (cal.selectedDate.getMonth() +1)+ "/" + cal.selectedDate.getDate() +"/"+cal.selectedDate.getFullYear();
		trace("DateField.selected date *CHANGE*" + selected);
		inputTxtA.text= selected;
		var e:EVTD = new EVTD();
		e.type ="change";
		e.target = this;
		this.dispatchEvent (e);
	}
	public function onUserSelectedDate(bypassEvent:Boolean):Void{
		trace("DateField.onUserSelectedDate");
	
		cal._visible = false;
		cal._height = choose_btn._height;
		cal._width = choose_btn._width;
		var e:EVTD = new EVTD();
		if(bypassEvent == null || bypassEvent == false){
		e.type ="focusOut";
		e.target = this;
		this.dispatchEvent (e);
		}
	}
	public function onOpenCalendar():Void{
		cal._height = cal._oheight;
		cal._width = cal._owidth;
		cal._visible = true;
	}
	public function getSelectedDate():TDate{
		return cal.selectedDate;
	}
	public function setSelectedDate(date:Date):Void{
		trace("DateField.setSelectedDateCHANGE");
		var t:TDate = new TDate();
		t.setTime(date.getTime());
		cal.selectedDate = t;
//				var e:EVTD = new EVTD();
//		e.type ="change";
//		e.target = this;
//		this.dispatchEvent (e);
	}
	public function onSetFocus():Void{
		trace("DateField.setFocus");
		var e:EVTD = new EVTD();
		e.type ="focusIn";
		e.target = this;
		this.dispatchEvent (e);
		onOpenCalendar();
	}
	public function onKillFocus():Void{
		trace("DateField.onKillFocus");

	
		if(!cal.hasFocus){
		 onUserSelectedDate(true);
		}
	}
	public function onChildClipLoad(_mc:MovieClip):Void{
		if(_mc == cal){

			onUserSelectedDate(true);
		}
	}
	/*.................................................................*/
	function s0_viewAssetsLoaded(e : AEvent) : Function
	{
		this.onFunctionEnter ("s0_viewAssetsLoaded-", e, []);
		switch (e)
		{
			case ENTRY_EVT :
			{
				inputTxtA.addEventListener("focusIn", TProxy.create(this, this.onSetFocus));
				var initObj:Object = new Object();
				initObj._x = choose_btn._x;
				initObj._y = choose_btn._y;
				cal = Calendar(attachMovie("TCalendar", "cal", getNextHighestDepth(), initObj));
				//snapshotDimensions(cal);
				cal.selectedDate.addEventListener(BaseModelObject.EVTD_MODEL_CHANGED, this, this.onSelectedDateChanged);
				cal.addEventListener(BaseModelObject.EVTD_MODEL_CHANGED, this, this.onUserSelectedDate);
		
				choose_btn.onRelease = TProxy.create(this, this.onOpenCalendar);
				return null;
			}
		}
		return super.s0_viewAssetsLoaded(e);
	}
}